package cn.czh0123.utils;

import cn.czh0123.msg.CodeMsg;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author: czh
 * @description: 封装返回结果
 * @date: 2024/3/24 14:06
 */
@Data
@NoArgsConstructor
public class ResultWrapper<T> {

    public static final int CODE_SUCCESS = 200;
    public static final String MSG_SUCCESS = "操作成功";

    public static final int CODE_ERROR = 500;
    public static final String MSG_ERROR = "系统异常，请联系管理员";

    private int code;
    private String msg;
    private T data;

    public ResultWrapper(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResultWrapper<T> success(T data) {
        return new ResultWrapper<>(CODE_SUCCESS, MSG_SUCCESS, data);
    }

    public static <T> ResultWrapper<T> success() {
        return new ResultWrapper<>(CODE_SUCCESS, MSG_SUCCESS, null);
    }

    public static <T> ResultWrapper<T> error(CodeMsg codeMsg) {
        return new ResultWrapper<>(codeMsg.getCode(), codeMsg.getMsg(), null);
    }

    public static <T> ResultWrapper<T> error(String msg) {
        return new ResultWrapper<>(CODE_ERROR, msg, null);
    }

    public static <T> ResultWrapper<T> defaultError() {
        return new ResultWrapper<>(CODE_ERROR, MSG_ERROR, null);
    }

}
